package chapter18;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Node {
    public Node root;
    public int n;//存储在节点中关键字的个数
    public List<Integer> keys = new ArrayList<>(); // 非降序排列
    public boolean leaf; // ture是叶子,false是内部节点
    // 指向其他孩子的指针
    public List<Node> c = new ArrayList<>();
    // 度,用于分裂
    public int t = 2;

    public Node(int n) {
        this.n = n;
    }

    public Node() {
    }

    public void setKey(int i, int key) {

    }

    public void addKey(int i, int key) {
        if (i <= keys.size() - 1 && keys.size() > 0) {
            keys.set(i, key);
        } else {
            keys.add(key);
        }
        keys = keys.stream().sorted().collect(Collectors.toList());
    }

    public void addC(int i, Node node) {
        Node newNode = new Node();
        newNode.n = node.n;
        newNode.keys = new ArrayList<>(node.keys);
        newNode.leaf = node.leaf;
        newNode.c = new ArrayList<>(node.c);
        if (i <= n - 1 && c.size() > 0) {
            c.set(i, newNode);
        } else {
            c.add(newNode);
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "root=" + root +
                ", n=" + n +
                ", keys=" + keys +
                ", leaf=" + leaf +
                ", c=" + c +
                '}';
    }
}
